<?php

/**
 * ApplicationCorporateIndividual form.
 *
 * @package    acca
 * @subpackage form
 * @author     Rommel M. Zamora / Sonny L. Cabali
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ApplicationCorporateIndividualForm extends BaseApplicationCorporateIndividualForm
{
  public function configure()
  {
    unset($this['created_at'], $this['updated_at'],$this['ref_number']);

    // set years to minimum of 18 years old
    $years = range(date('Y')-18, 1960);

    // set Labels
    $this->widgetSchema['globe_mobile_access_code']->setLabel('Globe mobile access code (Eg. 0917, 0906)');
    $this->widgetSchema['globe_mobile_phone_number']->setLabel('Globe mobile number<br />(This is the 7-digit numbers after the access code)');
    $this->widgetSchema['globe_telephone_access_code']->setLabel('Globe landline access code');
    $this->widgetSchema['globe_telephone_phone_number']->setLabel('Globe landline number');
    $this->widgetSchema['others_subscriber']->setLabel('Other subscriptions');
    $this->widgetSchema['rented_per_month']->setLabel('If rented, how much per month?');
    $this->widgetSchema['mortgage_per_month']->setLabel('If mortgage, how much per month?');
    $this->widgetSchema['home_address']->setLabel('Room/Flr/Bldg Name/No./Street');
    $this->widgetSchema['home_brgy_municipality_town']->setLabel('Brgy/Municipality/Town');
    $this->widgetSchema['home_city_province']->setLabel('City/Province');
    $this->widgetSchema['home_postal_code_zip_code']->setLabel('Postal/Zip code');
    $this->widgetSchema['foreigner_country']->setLabel('If foreigner, country?');
    $this->widgetSchema['spouse_profession_company_name']->setLabel('Spouse Profession/Company name');
    $this->widgetSchema['business_address']->setLabel('Room/Flr/Bldg Name/No./Street');
    $this->widgetSchema['business_brgy_municipality_town']->setLabel('Brgy/Municipality/Town');
    $this->widgetSchema['business_city_province']->setLabel('City/Province');
    $this->widgetSchema['business_postal_code_zip_code']->setLabel('Postal/Zip code');
    $this->widgetSchema['value_added_service_special_features']->setLabel('Value Added Service/ Special Features');
    $this->widgetSchema['handset_hbp']->setLabel('Handset HBP');
    $this->widgetSchema['gsis_sss']->setLabel('GSIS/SSS');
    $this->widgetSchema['tin']->setLabel('TIN');
    $this->widgetSchema['corporate_id']->setLabel('Corporate ID');
//    $this->widgetSchema['ref_number']->setDefault(RandomNameGenerator::transactionCode());
//    $this->widgetSchema['ref_number']->setAttribute('readonly','readonly');


    // Add email validation to email fields
    $emailFields = array(
      'email_address'
    );
    foreach($emailFields as $emailField) {
      $this->validatorSchema[$emailField] = new sfValidatorEmail($this->validatorSchema[$emailField]->getOptions());
    }

    $numbersOnlyFields = array(
      'globe_mobile_access_code',
      'globe_mobile_phone_number',
      'globe_telephone_access_code',
      'globe_telephone_phone_number',
      'home_postal_code_zip_code',
      'home_telephone_number',
      'spouse_telephone_number',
      'business_postal_code_zip_code',
      'authorized_contact_number',
      'handset_imei_number'
    );

    $lettersOnlyFields = array(
      'last_name',
      'first_name',
      'middle_name',
      'mothers_maiden_name',
      'citizenship',
      'foreigner_country',
      'spouse_name',
      'authorized_person_to_receive_delivery'
    );

    $validatorSchema = $this->validatorSchema;

    // Loop through fields and add validator classes for each
    foreach($validatorSchema->getFields() as $key=>$field) {
      $validators = array();

      // if required add class validate[required]
      if($field->getOption('required')) {
        $validators[] = 'required';
        $label = ($this->widgetSchema[$key]->getLabel()) ? $this->widgetSchema[$key]->getLabel() : $this->widgetSchema->getFormFormatter()->generateLabelName($key);
        $this->widgetSchema[$key]->setLabel($label.' *');
      }
      // if not required add class validate[optional]
      else {
        $validators[] = 'optional';
      }

      // add max_length based on length in schema
      if($field->getOption('max_length')) {
        $validators[] = 'maxSize['.$field->getOption('max_length').']';
      }

      // if integer, add class integer
      if(get_class($field) == 'sfValidatorInteger') {
        $validators[] = 'custom[integer]';
      }

      // if email, add class email
      if(get_class($field) == 'sfValidatorEmail') {
        $validators[] = 'custom[email]';
      }

      // if numbers only, add class onlyNumberSp
      if(in_array($key,$numbersOnlyFields)) {
        $validators[] = 'custom[onlyNumberSp]';
      }

      // if letters only, add class onlyNumberSp
      if(in_array($key,$lettersOnlyFields)) {
        $validators[] = 'custom[onlyLetterSp]';
      }


      $this->widgetSchema[$key]->setAttribute('class','validate['.implode(',', $validators).']');

    }

    // set birthday years and default value
    $this->widgetSchema['birthday']->setOption('years', array_combine($years, $years));
    $this->widgetSchema['birthday']->setDefault('01/01/1960');

    // set spouse birthday years
    $this->widgetSchema['spouse_birthday']->setOption('years', array_combine($years, $years));

    // add 'dependent' class to dependent fields (used for hiding/showing them in view)
    $dependentFields = array(
        'globe_mobile_access_code'=>'existing_subscriber',
        'globe_mobile_phone_number'=>'existing_subscriber',
        'globe_telephone_access_code'=>'existing_subscriber',
        'globe_telephone_phone_number'=>'existing_subscriber',
        'others_subscriber'=>'existing_subscriber',
        'spouse_name'=>'civil_status',
        'spouse_birthday'=>'civil_status',
        'spouse_profession_company_name'=>'civil_status',
        'spouse_position'=>'civil_status',
        'spouse_telephone_number'=>'civil_status'
        );
    foreach($dependentFields as $key=>$value) {
      $this->widgetSchema[$key]->setAttribute('class',$this->widgetSchema[$key]->getAttribute('class').' dependent');
      $this->widgetSchema[$key]->setAttribute('data-dependent','application_corporate_individual_'.$value);
    }


  }

}
